﻿''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' Magic Content - A desktopmodule to publish content and send out
' newsletters from within the DotNetNuke® Portal Framework
'
'
' Copyright(c) 2005-2006 proeder.network.consult
' Philipp Becker (philipp.becker@portalfabrik.net)
' Internet: http://www.portalfabrik.net
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Imports System
Imports System.Data

Namespace dnnWerk.Modules.Nuntio.Content

    Public Class ContentCollection

        Private _content As List(Of ContentInfo) = Nothing

        Public Sub New()
            _content = New List(Of ContentInfo)
        End Sub

        Public Sub SetList(ByVal ContentList As List(Of ContentInfo))            
            _content = ContentList
        End Sub

        Public Sub AddContent(ByVal objContent As ContentInfo)
            _content.Add(objContent)
        End Sub

        Public Sub Clear()
            _content.Clear()
        End Sub

        Public ReadOnly Property Count() As Integer
            Get
                Return _content.Count
            End Get
        End Property

        Public ReadOnly Property Items() As List(Of ContentInfo)
            Get
                Return _content
            End Get
        End Property

        Public Function GetContent(ByVal Position As Integer) As ContentInfo
            Return _content(Position)
        End Function

        Public Function FindItem(ByVal Locale As String, ByVal RoleName As String)
            Return FindItem(Locale, RoleName, Null.NullInteger)
        End Function

        Public Function FindItem(ByVal Locale As String, ByVal RoleName As String, ByVal Version As Integer) As ContentInfo

            'try finding a published content
            For Each objContent As ContentInfo In _content
                If objContent.Role.ToLower = RoleName.ToLower _
                    AndAlso objContent.Locale.ToLower = Locale.ToLower _
                    AndAlso (objContent.Version = Version Or Version = Null.NullInteger) _
                    AndAlso objContent.IsPublished Then

                    Return objContent

                End If
            Next

            'nothing found yet, lookup most recent item
            For Each objContent As ContentInfo In _content
                If objContent.Role.ToLower = RoleName.ToLower _
                    AndAlso objContent.Locale.ToLower = Locale.ToLower _
                    AndAlso (objContent.Version = Version Or Version = Null.NullInteger) Then

                    Return objContent

                End If
            Next

            Return Nothing

        End Function

        Public Function FindItemById(ByVal ItemId As Integer) As ContentInfo
            For Each objContent As ContentInfo In _content
                If objContent.ContentId = ItemId Then
                    Return objContent
                End If
            Next
            Return Nothing
        End Function

        Public Function GetNextVersion(ByVal Role As String, ByVal Locale As String) As Integer

            Dim i As Integer = 0

            For Each oContent As ContentInfo In Me.Items
                If oContent.Locale.ToLower = Locale.ToLower AndAlso oContent.Role.ToLower = Role.ToLower Then
                    i = i + 1
                End If
            Next

            Return i + 1

        End Function

        Public Function HighestPriority() As Integer

            Dim i As Integer = 0

            For Each oContent As ContentInfo In Me.Items
                If oContent.Priority > i Then
                    i = oContent.Priority
                End If
            Next

            Return i

        End Function

        Public Function GetNextPriority(ByVal Role As String) As Integer

            'default prio
            Dim i As Integer = 0
            'Dim strLastRole As String = ""
            'Dim blnNewRoleContent As Boolean = False

            'For Each oContent As ContentInfo In Me.Items
            '    strLastRole = oContent.Role

            '    'seems as if we have a new content for a new role
            '    If Role.ToLower <> strLastRole.ToLower Then
            '        blnNewRoleContent = True
            '    End If

            'Next

            'If blnNewRoleContent Then
            '    'get next prio number
            '    i = HighestPriority() + 1
            'End If

            If IsRoleInList(Me.Items, Role) Then
                i = GetContentByRole(Role)(0).Priority
            Else
                i = HighestPriority() + 1
            End If

            Return i

        End Function

        Public Function GetRoleContentUnique() As List(Of ContentInfo)

            Dim lst As New List(Of ContentInfo)

            For Each oContent As ContentInfo In Me.Items
                If Not IsRoleInList(lst, oContent.Role) Then
                    lst.Add(oContent)
                End If
            Next

            Return lst

        End Function

        Public Function GetContentByRole(ByVal Role As String) As List(Of ContentInfo)

            Dim lst As New List(Of ContentInfo)

            For Each oContent As ContentInfo In Me.Items
                If oContent.Role.ToLower = Role.ToLower Then
                    lst.Add(oContent)
                End If
            Next

            Return lst

        End Function

        Public Function IsRoleInList(ByVal lst As List(Of ContentInfo), ByVal role As String) As Boolean
            For Each oContent As ContentInfo In lst
                If oContent.Role.ToLower = role.ToLower Then
                    Return True
                End If
            Next
            Return False
        End Function

    End Class

End Namespace
